import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class TestServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取 POST 请求的参数
        String param1 = request.getParameter("param1");
        String param2 = request.getParameter("param2");

        String httpMethod = request.getMethod();
        result = "Received " + httpMethod + " request with params: param1=" + param1 + ", param2=" + param2;

        // 获取请求头信息
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String headerName = headerNames.nextElement();
            String headerValue = request.getHeader(headerName);
            result += ", " + headerName + "=" + headerValue;
        }

        // 处理参数并生成响应
        String result = "Received POST request with params: param1=" + param1 + ", param2=" + param2;

        // 设置响应类型为 text/plain
        response.setContentType("text/plain");
        PrintWriter out = response.getWriter();
        out.println(result);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取 GET 请求的参数
        String param1 = request.getParameter("param1");
        String param2 = request.getParameter("param2");

        String httpMethod = request.getMethod();
        result = "Received " + httpMethod + " request with params: param1=" + param1 + ", param2=" + param2;

        // 获取请求头信息
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String headerName = headerNames.nextElement();
            String headerValue = request.getHeader(headerName);
            result += ", " + headerName + "=" + headerValue;
        }

        // 处理参数并生成响应
        String result = "Received GET request with params: param1=" + param1 + ", param2=" + param2;

        // 设置响应类型为 text/plain
        response.setContentType("text/plain");
        PrintWriter out = response.getWriter();
        out.println(result);
    }
}